package com.diy.sigmund.entity;

import java.util.Objects;

/**
 * @author ylm-sigmund
 * @since 2021/4/17 16:01
 */
public class ResultMsg  {

    private static final long serialVersionUID = 1L;
    /** 日志链路追踪id */
    private String traceId;
    /**
     * 响应码，200表示成功，其它表示失败
     */
    private String code;
    /**
     * 提示信息
     */
    private String msg;
    /**
     * 响应数据
     */
    private Object data;

    /**
     * 补齐无参构造，解决反序列化失败
     * Cannot construct instance of `com.diy.sigmund.diycommon.entity.ResultMsg` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
     */
    public ResultMsg() {
    }

    public ResultMsg(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /** 成功响应 */
    public static ResultMsg success() {
        return new ResultMsg(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), null);
    }

    /**
     * 成功响应
     *
     * @param data data
     * @return ResultMsg
     */
    public static ResultMsg success(Object data) {
        return new ResultMsg(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), data);
    }

    /** 失败响应 */
    public static ResultMsg fail(ResponseEnum responseEnum) {
        return new ResultMsg(responseEnum.getCode(), responseEnum.getMsg(), null);
    }

    /** 失败响应 */
    public static ResultMsg fail(String msg) {
        return new ResultMsg(ResponseEnum.REQUEST_FAIL.getCode(), msg, null);
    }

    /** 失败响应 */
    public static ResultMsg fail(String msg, Object data) {
        return new ResultMsg(ResponseEnum.REQUEST_FAIL.getCode(), msg, data);
    }

    /**
     * 失败响应
     *
     * @param responseEnum 响应枚举
     * @param data         data
     * @return ResultMsg
     */
    public static ResultMsg fail(ResponseEnum responseEnum, Object data) {
        return new ResultMsg(responseEnum.getCode(), responseEnum.getMsg(), data);
    }

    /**
     * 失败响应
     *
     * @param code code
     * @param msg  msg
     * @return ResultMsg
     */
    public static ResultMsg fail(String code, String msg) {
        return new ResultMsg(code, msg, null);
    }

    public boolean hasSuccess() {
        return Objects.equals(ResponseEnum.SUCCESS.getCode(), this.getCode());
    }

    public boolean unSuccess() {
        return !hasSuccess();
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
